package org.gd.hellojavacore.tests;

import com.google.common.base.Strings;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicLong;

public class AtomicTest {

    public static void main(String...a) throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger(0);
        while(atomicInteger.addAndGet(1) < 10) {
            System.out.println(atomicInteger.get());
        }

        AtomicIntegerArray array = new AtomicIntegerArray(10);
        System.out.println(array);

        for(int i=0; i<array.length(); i++) {
            array.addAndGet(i, i);
        }
        System.out.println(array);


        long t = System.currentTimeMillis();
        Thread.sleep((long) (Math.random()*100));
        AtomicLong aLong = new AtomicLong(System.currentTimeMillis());
        System.out.printf("\n\n%d\n\n", aLong.addAndGet(-t));





        String response = null;
        final AtomicInteger count = new AtomicInteger(0);

        while(Strings.isNullOrEmpty(response) && count.incrementAndGet()<3) {
            response = "124124";
            System.out.printf("[count]: %d\n", count.get());
        }
        System.out.printf("[result]: %s\n", response);


    }
}
